from tools.db_utils import insert_single_row_and_get_id
from tools.parse_week_ranges import parse_week_ranges
from tools.db_utils import db_operation,execute_single_value_query
from tools.map_list_of_dictionary import map_list_of_dictionary
from tools.parse_class_time import parse_class_time
from tools.split_row import split_row

SQL_插入课堂安排 = "INSERT INTO course_schedule (course_name,clazz_id,week,day,start,end) VALUES (%(course_name)s,%(clazz_id)s,%(week)s,%(day)s,%(start)s,%(end)s)"
def insertCourseSchedule(list_data):
    map_list_of_dictionary(list_data, {"course_name": "B", "week": "O", "day": "AA", "start": "AB", "end": "AC"})
    split_row(list_data, parse_week_ranges, 'O')
    clazz_name=list_data[0]["A"]
    # 获取clazzId,根据data_list中的班级名clazz_name
    clazz_id=execute_single_value_query(f"select id from clazz where name= '{clazz_name}'")
    for row in list_data:
        (day,start,end)=parse_class_time(row["Q"])
        row.update({"day":day,"start":start,"end":end,"week":row["O"]})
        row.update({"clazz_name":row['A'],"course_name":row["B"],"clazz_id":clazz_id})
        # 插入到course_schedule表中,
        insert_single_row_and_get_id(SQL_插入课堂安排, **row)

    #并返回该班级的课堂安排数量
    return f'SELECT COUNT(*) FROM course_schedule where clazz_id={clazz_id}'
